package com.bright.ghj.overt.service.impl;

import com.bright.ghj.overt.pojo.command.ImportAccessCommand;
import com.bright.ghj.overt.pojo.vo.AsyncTaskResultVO;
import com.bright.ghj.overt.pojo.vo.CollectPlatformAsyncTaskVO;
import com.bright.ghj.overt.service.AccessImportService;
import com.bright.ghj.overt.service.AsyncMessageService;
import com.bright.ghj.overt.service.BaseDataService;
import com.bright.ghj.overt.task.timely.AsyncAccessImportExecutor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author hxj
 * @Date 2024/2/27 16:57
 * @Description
 */
@Component
@RequiredArgsConstructor
@Qualifier("asyncAccessImport")
public class AsyncAccessImportServiceImpl implements AccessImportService<AsyncTaskResultVO> {

    private final AsyncMessageService asyncMessageService;
    private final BaseDataService baseDataService;


    @Override
    public List<AsyncTaskResultVO> startImport(ImportAccessCommand command, File accessFile, String userName) {

//        String username = command.getUserName();

        String keyword = UUID.randomUUID().toString();

        //1.2. 校验通过生成一个记录信息，可以让用户实时感知到上传的情况
        //1.3. 异步执行任务、监控该任务执行的状态和进度、 还要能够查看实际的采集情况
        AsyncAccessImportExecutor asyncTaskDTO = new AsyncAccessImportExecutor();
        asyncTaskDTO.setKeyword(keyword);
        asyncTaskDTO.setTaskType("导入Access");
        asyncTaskDTO.setUsername(userName);
        asyncTaskDTO.setTaskName("CS版财务数据导入");

        //设置任务的事务管理器
        asyncTaskDTO.setBaseDataService(baseDataService);
        asyncTaskDTO.setImportAccessCommand(command);
        asyncTaskDTO.setAccessFile(accessFile);

        /*
         * 同步锁的标识值 这里任务根据文件名来锁
         */
        asyncTaskDTO.setSyncKeyword(accessFile.getName());

        //开始异步执行任务
        CollectPlatformAsyncTaskVO collectPlatformAsyncTaskVO = new CollectPlatformAsyncTaskVO();
        collectPlatformAsyncTaskVO.setKeyword(keyword);

        AsyncTaskResultVO asyncTaskResultVO = asyncMessageService.execAsyncTask(asyncTaskDTO);
        List<AsyncTaskResultVO> asyncTaskResultVOS = new ArrayList<>();
        //如果任务成功提交
//        if (asyncTaskResultVO != null && asyncTaskResultVO.getIsSubmitTask()) {
//            // 任务成功提交，正在等待执行
//            saveResultVO.setSuccess(true);
//        } else {
//            collectPlatformAsyncTaskVO.setStatus(ABANDON_EXECUTION.getMessage());
//            collectPlatformAsyncTaskVO.setRemark("执行失败");
//            collectPlatformAsyncTaskVO.setNoSubmitIllustrate(asyncTaskResultVO != null ? asyncTaskResultVO.getNoSubmitIllustrate() : null);
//        }
        asyncTaskResultVOS.add(asyncTaskResultVO);
        return asyncTaskResultVOS;


    }

}
